package com.zeng.jiudian.service.impl;

import com.zeng.jiudian.bean.NavModel;
import com.zeng.jiudian.dao.NavDao;
import com.zeng.jiudian.dao.impl.NavDaoImpl;
import com.zeng.jiudian.service.NavService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/28 - 16:40
 */
public class NavServiceImpl implements NavService
{
    @Override
    public int add(NavModel navModel)
    {
        NavDao navDao = new NavDaoImpl();

        int count = navDao.add(navModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<NavModel> findAll(NavModel navModel)
    {
        NavDao navDao = new NavDaoImpl();

        return navDao.findAll(navModel);
    }

    @Override
    public int getCount(NavModel navModel)
    {
        NavDao navDao = new NavDaoImpl();

        return navDao.getCount(navModel);
    }

    @Override
    public int delete(int id)
    {
        NavDao navDao = new NavDaoImpl();

        int count = navDao.delete(id);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public NavModel findById(int id)
    {
        NavDao navDao = new NavDaoImpl();

        return navDao.findById(id);
    }

    @Override
    public int update(NavModel navModel)
    {
        NavDao navDao = new NavDaoImpl();

        if ("".equals(navModel.getNavName()))
        {
            navModel.setNavName(null);
        }
        if ("".equals(navModel.getNavHref()))
        {
            navModel.setNavHref(null);
        }
        if (navModel.getSort() < 0)
        {
            navModel.setSort(null);
        }

        int count = navDao.update(navModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<NavModel> getAll()
    {
        List<NavModel> list = null;
        Jedis jedis = new Jedis();
        jedis.select(0);
        Long length = jedis.llen("navId");
        if (length > 0)
        {
            list = new ArrayList<>();
            List<String> navIds = jedis.lrange("navId", 0, length);
            List<String> navName = jedis.lrange("navName", 0, length);
            List<String> navHref = jedis.lrange("navHref", 0, length);
            List<String> sort = jedis.lrange("sort", 0, length);
            List<String> createTime = jedis.lrange("navCreateTime", 0, length);
            List<String> updateTime = jedis.lrange("navUpdateTime", 0, length);
            for (int i = 0; i < length; i++)
            {
                NavModel model = new NavModel();
                model.setId(Integer.parseInt(navIds.get(i)));
                model.setNavName(navName.get(i));
                model.setNavHref(navHref.get(i));
                model.setSort(Integer.parseInt(sort.get(i)));
                model.setCreateTime(createTime.get(i));
                model.setUpdateTime(updateTime.get(i));
                list.add(model);
            }
        }
        else
        {
            NavDao navDao = new NavDaoImpl();
            list = navDao.getAll();
            jedis.del("navId", "navName", "navHref", "sort", "navCreateTime", "navUpdateTime");
            for (NavModel model : list)
            {
                jedis.rpush("navId", model.getId().toString());
                jedis.rpush("navName", model.getNavName());
                jedis.rpush("navHref", model.getNavHref());
                jedis.rpush("sort", model.getSort().toString());
                jedis.rpush("navCreateTime", model.getCreateTime());
                jedis.rpush("navUpdateTime", model.getUpdateTime());
            }
        }


        return list;
    }
}
